﻿namespace KinonekoSoftware.Studio.ViewModels.Main
{
    public class RepositoryHomeViewModel : RepositoryGalleryItem
    {
        private readonly IRepository           _repository;

        //-------------------------------------------------------------
        //
        //          Constructor
        //
        //-------------------------------------------------------------
        public RepositoryHomeViewModel()
        {
            _repository                    = Platform.GetRepository();
            SaveRepositoryChangedCommand   = CreateCommand(DoSaveRepositoryChangedCommand);
            LocateLogsFolderCommand        = CreateCommand(DoLocateLogsFolderCommand);
            LocateSettingsFolderCommand    = CreateCommand(DoLocateSettingsFolderCommand);
            LocateApplicationFolderCommand = CreateCommand(DoLocateApplicationFolderCommand);

            GotoChannelTalkingCommand = CreateCommand(DoGotoChannelTalkingCommand);
            GotoFriendTalkingCommand  = CreateCommand(DoGotoFriendTalkingCommand);
            GotoForumCommand          = CreateCommand(DoGotoForumCommand);



            RebuildCatalogCommand = CreateCommand(DoRebuildCatalogCommand);
        }

        //-------------------------------------------------------------
        //
        //          Constructor
        //
        //-------------------------------------------------------------
    

        //-------------------------------------------------------------
        //
        //          Command Handlers
        //
        //-------------------------------------------------------------

        private static void DoLocateLogsFolderCommand()
        {
            var folder = Ioc.Get<ApplicationInformation>().LogsFolder;
            Platform.OpenLink(folder);
        }
        
        private static void DoLocateSettingsFolderCommand()
        {
            var folder = Ioc.Get<ApplicationInformation>().SettingFolder;
            Platform.OpenLink(folder);
        }

        private static void DoLocateApplicationFolderCommand()
        {
            var folder = AppDomain.CurrentDomain.BaseDirectory;
            Platform.OpenLink(folder);
        }

        private async void DoSaveRepositoryChangedCommand()
        {
            //
            //
            _repository.UpdateProperty();
            
            //
            //
            RaiseUpdated(nameof(RepositoryID));
            
            await AppViewModel.Snapshot(_repository);
            await Success(TitleSR.SuccessOperation, TextSR.Successful_Save);
        }


        private void DoRebuildCatalogCommand()
        {
            var repository = Platform.GetRepository();
            var msg        = new ApplyCatalogChangedMessage();

            if (repository.Has<CatalogRoot>())
            {
                repository.Unset<CatalogRoot>(out _);
                repository.GetOrCreate<CatalogRoot>(CatalogRoot.CreateCatalogsVersion2, false);
            }

            Queue.OnNext(msg);
        }


        private void DoGotoChannelTalkingCommand()
        {
            var parent = Ioc.Get<AppViewModel>();

            if (parent.Current is not RepositoryGalleryViewModel)
            {
                //
                // 跳转到启动页
                parent.Current = parent.Inboard
                                       .OfType<RepositoryGalleryViewModel>()
                                       .First();
            }

            //
            //
            var gallery = (RepositoryGalleryViewModel)Core;
        }

        private void DoGotoFriendTalkingCommand()
        {
            var parent = Ioc.Get<AppViewModel>();

            if (parent.Current is not RepositoryGalleryViewModel)
            {
                //
                // 跳转到启动页
                parent.Current = parent.Inboard
                                       .OfType<RepositoryGalleryViewModel>()
                                       .First();
            }

            //
            //
            var gallery = (RepositoryGalleryViewModel)Core;
        }

        private void DoGotoForumCommand()
        {
            var parent = Ioc.Get<AppViewModel>();

            if (parent.Current is not RepositoryGalleryViewModel)
            {
                //
                // 跳转到启动页
                parent.Current = parent.Inboard
                                       .OfType<RepositoryGalleryViewModel>()
                                       .First();
            }

            //
            //
            var gallery = (RepositoryGalleryViewModel)Core;
        }

        public string RepositoryID => _repository.DatabaseID;

        public string RepositoryName
        {
            get => _repository.Name;
            set
            {
                _repository.Name = value;
                RaiseUpdated();
            }
        }
        
        public string RepositoryForeignName
        {
            get => _repository.ForeignName;
            set
            {
                _repository.ForeignName = value;
                RaiseUpdated();
            }
        }
        
        public string RepositoryAuthor
        {
            get => _repository.Author;
            set
            {
                _repository.Author = value;
                RaiseUpdated();
            }
        }

        /// <summary>
        /// 获得AppViewModel
        /// </summary>
        public AppViewModel AppViewModel => Ioc.Get<AppViewModel>();

        public ObservableCollection<RepositoryCache> Repositories                => AppViewModel.Repositories;
        public ICommandEX                            GotoSettingCommand          => AppViewModel.GotoSettingCommand;
        public ICommandEX                            GotoTutorialCommand         => AppViewModel.GotoTutorialCommand;
        public ICommandEX                            GotoBugReportCommand        => AppViewModel.GotoBugReportCommand;
        public ICommandEX                            LocateRepositoryCommand     => AppViewModel.LocateRepositoryCommand;
        public ICommandEX                            ExportRepositoryWikiCommand => AppViewModel.ExportRepositoryWikiCommand;
        public ICommandEX                            SwitchRepositoryCommand     => AppViewModel.SwitchRepositoryCommand;
        public ICommandEX                            SyncRepositoryCommand       => AppViewModel.SyncRepositoryCommand;
        public ICommandEX                            DeleteRepositoryCommand     => AppViewModel.DeleteRepositoryCommand;
        
        //-------------------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------------------

        public ICommandEX GotoChannelTalkingCommand    { get; }
        public ICommandEX GotoFriendTalkingCommand     { get; }
        public ICommandEX GotoForumCommand             { get; }
        public ICommandEX SaveRepositoryChangedCommand { get; }
        public ICommandEX LocateLogsFolderCommand      { get; }
        public ICommandEX LocateSettingsFolderCommand      { get; }
        public ICommandEX LocateApplicationFolderCommand      { get; }
        

        public ICommandEX RebuildCatalogCommand { get; }
    }
}